package org.xx.armory.commons;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 具有生命周期的对象的观察器。
 */
public class LifeCycleObserver<T extends LifeCycle> {
    private Set<T> objects;

    public LifeCycleObserver() {
        this.objects = new LinkedHashSet<>();
    }

    public synchronized void afterInitialized(
            T o
    ) {
        this.objects.add(o);
    }

    public synchronized void beforeDisposing(
            T o
    ) {
        this.objects.remove(o);
    }

    public synchronized List<T> dumpAll() {
        return new ArrayList<>(this.objects);
    }
}
